import {goodslist,goodstotal} from "../../requset/api"
export const state={
    list:[],
    total:0,
    size:2,
    page:1
}

export const mutations={
    changeGoods(state,arr){
        state.list=arr
    },
    changeTotal(state,num){
        state.total=num
    },
    changePage(state,num){
        state.page=num
    },
    changeSize(state,num){
        state.Size=num
    }
}

export const  actions={
        reqChangeGoods(context,flag){
            let data=flag?{}:{size:context.state.size,page:context.state.page}
            goodslist(data).then(res=>{
                let list =res.data.list?res.data.list:[]
                if(list.length==0&&context.state.page>1){
                    context.commit("changePage",context.state.page-1)
                    context.dispatch("reqChangeGoods")
                }
                list.forEach(item => {
                    item.specsattr = JSON.parse(item.specsattr)
                });
                context.commit("changeGoods",list)
            })
        },
        reqChangeTotal(context){
            goodstotal().then(res=>{
                context.commit("changeTotal",res.data.list[0].total)
            })
        },
        reqChangePage(context,num){
            context.commit("changePage",num)
            context.dispatch("reqChangeGoods")
        },
        reqChangeSize(context,num){
            context.commit("changeSize",num)
            context.dispatch("reqChangeGoods")
        }
}

export const getters={
    list(state){
        return state.list
    },
    total(state){
        return state.total
    },
    size(state){
        return state.size
    }
}

export default{
    state,
    mutations,
    actions,
    getters,
    namespaced:true
}